<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Majors extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model("subject");
	}

	function _remap($param) {
		$params = array();

		$params["breadcrumbs"] = array(
			site_url() => "Home",
		);

		if($param and $param != "index") {
			$this->load->model("major");

			$getSubject = $this->subject->getSubject($where = "a.sub_url_name = '".$this->db->escape_str($param)."' and e2c_id is not null");
			if(count($getSubject)) {
				if(!$this->input->is_ajax_request()) {
					$params["ENTITY"] = $getSubject[key($getSubject)]["children"][key($getSubject[key($getSubject)]["children"])]["subName"];
					$params["breadcrumbs"][site_url()."majors"] = $this->lang->line("MAJOR");
					$params["breadcrumbs"][] = $params["ENTITY"];

					// top institutes
					$this->load->model("institute");
					$getInstitute = $this->institute->getInstitute($where = "ins_presenter ".(($this->uri->segment(1) == "preview")?(" in ('I', 'N') "):(" = 'I' "))." and e2c_id is not null and ins_id is not null and sub_url_name = '".$this->db->escape_str($param)."'", $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "rand()", $dir = "DESC");
					$selectedInstitutes = array();
					$arrInstitutes = array();
					if(count($getInstitute)) {
						$sumPriority = 0;
						foreach($getInstitute as $d) {
							$sumPriority += $d["insPriority"];
						}
						for($i = 0; $i < 5; $i++) {
							$rand = rand(0, $sumPriority);
							$aktSumPriority = 0;
							foreach($getInstitute as $ii => $d) {
								if(!in_array($d["insId"], $selectedInstitutes)) {
									$aktSumPriority += $d["insPriority"];
									if($aktSumPriority >= $rand) {
										$selectedInstitutes[] = $d["insId"];
										$arrInstitutes[$d["couUrlName"]]["couName"] = $d["couName"];
										$arrInstitutes[$d["couUrlName"]]["conUrlName"] = $d["conUrlName"];
										$arrInstitutes[$d["couUrlName"]]["city"][$d["cityUrlName"]]["ins"][$d["insUrlName"]]["insName"] = $d["insName"];
										$sumPriority -= $d["insPriority"];
										break;
									}
								}
							}
						}
					}
					$params["topInstitutes"] = $arrInstitutes;

					$params["getInstitute"] = $getInstitute;
					$params["google_map_big"] = $this->load->view("page/google_map", $params, TRUE);
				}
				
				$params["getAvailableProgramsBySubject"] = $this->major->getAvailableProgramsBySubject($where = "maj_presenter ".(($this->uri->segment(1) == "preview")?(" in ('I', 'N') "):(" = 'I' "))." and sub_url_name = '".$param."'", $start = ($this->input->post("page", 0)*$this->config->item("availableProgramPageSize")), $limit = $this->config->item("availableProgramPageSize"));
				if(!$this->input->is_ajax_request()) {
					$this->load->model("article_model");
					$params["getArticleByEntId"] = $this->article_model->getArticleByEntId("e2a_sub_id = ".$getSubject[key($getSubject)]["children"][key($getSubject[key($getSubject)]["children"])]["subId"]);
					$params["sidebar"] = $this->load->view("page/sidebar", $params, TRUE);
					$params["content"] = $this->load->view("page/show_major_by_subject", $params, TRUE);
				} else {
					$this->load->view("page/show_major_by_subject", $params);
				}
			} else {
				redirect("errors/error_404");
				$param = "index";
			}
		}

		if($param == "index") {
			$params["breadcrumbs"][] = $this->lang->line("MAJOR");
			$params["getSubject"] = $this->subject->getSubject($where = "maj_presenter = 'I' and maj_del = 0");
			$params["getSubjectKiemelt"] = $this->subject->getSubject($where = "maj_presenter ".(($this->uri->segment(1) == "preview")?(" in ('I', 'N') "):(" = 'I' "))." and maj_del = 0 and con_id is not null", $start = 0, $limit = 5, $fields = NULL, $query = NULL, $order = "rand()");
			$params["content"] = $this->load->view("page/subject", $params, TRUE);
		}

		if(!$this->input->is_ajax_request()) {
			$this->load->view("template/page", $params);
		}
	}

}